package com.mcxx.modules.minimumliving.controller;

import com.mcxx.modules.minimumliving.service.SuspendManagementService;
import com.mcxx.modules.base.service.read.SuspendFamilyReadService;
import com.mcxx.modules.miniSalvation.family.dto.SuspendFamilyResultDTO;
import com.mcxx.modules.miniSalvation.family.param.BatchProtectParam;
import com.mcxx.modules.miniSalvation.family.param.ExpireFamilyParam;
import com.mcxx.modules.miniSalvation.family.param.SuspendFamilyResultParam;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.*;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@Validated
@Api("暂停管理")
@RestService({"minimumliving/suspend/v2"})
public class SuspendManagementController extends DefaultRestService {
    private static final Logger logger = LoggerFactory.getLogger(SuspendManagementController.class);
    @Autowired
    private SuspendManagementService exSuspendManagementService;
    @Autowired
    private SuspendFamilyReadService suspendFamilyReadService;


    @PostMapping(value = "suspendList")
    @ApiOperation(value = "暂停家庭查询")
    public void suspendList(BatchProtectParam param, PageParam pageParam) {
        logger.info("暂停家庭查询,条件是{}", param);
        param.setState("2");
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        param.setUserLevel(Integer.parseInt(getCurUserAccount().getAreaLevel()));
        Page page = exSuspendManagementService.querySuspendList(param, getCurUserAccount(), pageParam);
        this.setPageBean(page);
        logger.info("暂停家庭查询,结果是{}", page);
    }


    @PostMapping(value = "querySuspendFamilyList")
    @ApiOperation(value = "暂停家庭管理")
    public void querySuspendFamilyList(BatchProtectParam param, PageParam pageParam) {
        logger.info("暂停家庭管理,条件是{}", param);
        param.setState("1");
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        param.setUserLevel(Integer.parseInt(getCurUserAccount().getAreaLevel()));
        Page page = exSuspendManagementService.querySuspendList(param, getCurUserAccount(), pageParam);
        this.setPageBean(page);
        logger.info("暂停家庭管理,结果是{}", page);
    }


    @PostMapping(value = "houseHoldQuit")
    @ApiOperation(value = "暂停的家庭整户停保")
    public void houseHoldQuit(@V({"notnull"}) String id) {
        logger.info("暂停的家庭整户停保,参数是{}", id);
        String res = exSuspendManagementService.houseHoldQuit(id,getCurUserAccount());
        setData(res);
        logger.info("暂停的家庭整户停保结束.结果是{}",res);
    }


    @PostMapping(value = "queryZanTingList")
    @ApiOperation(value = "救助家庭暂停")
    public void queryZanTingList(BatchProtectParam param, PageParam pageParam) {
        logger.info("救助家庭暂停,条件是{}", param);
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page page = exSuspendManagementService.queryZanTingList(param, getCurUserAccount(), pageParam);
        this.setPageBean(page);
        logger.info("救助家庭暂停,结果是{}", page);
    }


    @RequestMapping(value = "suspendFamily")
    @ApiOperation(value = "救助家庭列表暂停操作按钮")
    public void suspendFamily(@V({"notnull"})ExpireFamilyParam param) {
        logger.info("救助家庭列表操作按钮,条件是{}", param);
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        //param.setStopMonth(param.getStopMonth().replaceAll("-", ""));
        param.setStopMonth(param.getStopMonth());
        BaseResult<Boolean> baseResult = exSuspendManagementService.suspendFamily(param, getCurUserAccount());
        setData(baseResult);
        logger.info("救助家庭列表暂停操作按钮,结果是{}", baseResult);
    }


    @PostMapping(value = "repealSuspend")
    @ApiOperation(value = "暂停管理/撤销暂停")
    public void repealSuspend(@V({"notnull"}) String ids) {
        logger.info("暂停管理/撤销暂停,条件是{}", ids);
        BaseResult<Boolean> baseResult = exSuspendManagementService.repealSuspend(ids, getCurUserAccount());
        setData(baseResult.getData());
        logger.info("暂停管理/撤销暂停,结果是{}", baseResult);
    }


    @PostMapping(value = "updateSuccour")
    @ApiOperation(value = "恢复救助")
    public void updateSuccour(@V({"notnull"}) SuspendFamilyResultParam param) {
        logger.info("恢复救助,条件是{}", param);
//        if(Constant.UserLevel.STREET.equals(getCurUserAccount().getAreaLevel())){
            param.setReissueMonth(param.getEndMonth());
            param.setEndMonth(param.getEndMonth());
//            if (Integer.parseInt(param.getEndMonth()) - Integer.parseInt(DateUtil.getMonthString()) < 0) {





        param.setBusinessType(Constant.BusinessCode.DIBAO);
        BaseResult<Boolean> baseResult = exSuspendManagementService.recoverySuspend(param, getCurUserAccount());
        setData(baseResult);
        logger.info("恢复救助,结果是{}", baseResult);
    }


    @PostMapping(value = "termination")
    @ApiOperation(value = "终止救助")
    public void termination(@V({"notnull"}) SuspendFamilyResultParam param) {
        logger.info("终止救助,条件是{}", param);
        UserAccountVo userAccountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        BaseResult<Boolean> baseResult = exSuspendManagementService.terminationSuspend(param, userAccountVo);
        setData(baseResult);
        logger.info("终止救助,结果是{}", baseResult);
    }


    @PostMapping("downLoadSuspendMemberList")
    @ApiOperation(value="导出暂停信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出暂停信息")
    public void downLoadSuspendMemberList(HttpServletResponse response, @RequestBody BatchProtectParam param)throws IOException {
        logger.info("开始查询导出暂停信息，参数是：{}", param);
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        ExcelInfo excelInfo = exSuspendManagementService.exportExcelSuspendMember(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("开始导出暂停信息，查询结果是：{}", excelInfo);
    }


    @PostMapping("findSuspendContent")
    @ApiOperation(value="获取暂停申请原因", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="获取暂停申请原因")
    public void findSuspendContent(String id){
        logger.info("开始查询导出暂停信息，参数是：{}", id);
        SuspendFamilyResultDTO suspendContent = suspendFamilyReadService.findSuspendContent(id);
        setData(suspendContent);
        logger.info("开始导出暂停信息，查询结果是：{}", suspendContent);
    }


    @PostMapping("findFundPayDate")
    @ApiOperation(value="校验名册", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="校验名册")
    public void findFundPayDate(String orderId,String selectType){
        logger.info("开始查询导出暂停信息，参数是：{}", orderId);
        Map<String,String> res = exSuspendManagementService.findFundPayDate(orderId,selectType);
        setData(res);

        logger.info("开始导出暂停信息，查询结果是：{}", res);
    }

}
